home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 03 Demos and Info / CRCXMO.TXT < prev    next >
Encoding:
Text File  |  2019-04-13  |  18.3 KB  |  604 lines

  1.  
  2. MODEM PROTOCOL DOCUMENTATION
  3. By Ward Christensen     1/1/82
  4.  
  5. I will maintain a master copy of this. 
  6. Please pass on changes or suggestions
  7. via CBBS/Chicago at (312) 545-8086,
  8. CBBS/CPMUG (312) 849-1132 or by voice
  9. at  (312) 849-6279.
  10. Last Revision: 6/18/85  By Henry C.
  11. Schmitt.---State Table Appendix.
  12. Previous Revisions: 1/13/85 By John
  13. Byrns.---CRC Option Addendum.
  14. 8/9/82  By Ward Christensen.---Change
  15. ACK to 06H (from 05H).
  16.  
  17.  This version of the document was
  18. downloaded from the CBBS/CPMUG on
  19. 6/13/85 and the addition of the
  20. revision marks (') and minor
  21. editorial changes were made by Henry
  22. C. Schmitt.  Many people ask me for
  23. documentation on my modem protocol,
  24. i.e. the one used  in  the various
  25. modem programs in CPMUG, on volumes 6,
  26. 25, 40, 47... so here  it is.  At the
  27. request of Rick Mallinak on behalf of
  28. the guys at Standard Oil  with IBM
  29. P.C.s, as well as several previous
  30. requests, I finally decided to  put my
  31. modem protocol into writing.  It had
  32. been previously formally published
  33. only in the AMRAD newsletter.
  34.  
  35. Table of Contents
  36. 1.  DEFINITIONS
  37. 2.  TRANSMISSION MEDIUM LEVEL PROTOCOL
  38. 3.  MESSAGE BLOCK LEVEL PROTOCOL
  39. 4.  FILE LEVEL PROTOCOL
  40. 5.  DATA FLOW EXAMPLE INCLUDING ERROR
  41. RECOVERY
  42. 6.  PROGRAMMING TIPS.
  43. 7.  OVERVIEW OF CRC OPTION
  44. 8.  MESSAGE BLOCK LEVEL PROTOCOL, CRC
  45. MODE
  46. 9.  CRC CALCULATION
  47. 10.  FILE LEVEL PROTOCOL, CHANGES FOR
  48. COMPATIBILITY
  49. 11.  DATA FLOW EXAMPLES WITH CRC OPTION
  50.  
  51.  Appendix 1.  MODEM PROTOCOL STATE
  52. TABLE
  53.  
  54.  1. DEFINITIONS.  <SOH> 01H   <EOT>  
  55. 04H  <ACK>     06H   <NAK>     15H  
  56. <CAN>     18H  <C>     43H
  57.  
  58.  2. TRANSMISSION MEDIUM LEVEL PROTOCOL 
  59. Asynchronous, 8 data bits, no parity,
  60. one stop bit.
  61.  
  62. The protocol imposes no restrictions on
  63. the contents of the data being
  64. transmitted.  No control characters
  65. are looked for in the 128-byte data
  66. messages.  Absolutely any kind of data
  67. may be sent - binary, ASCII, etc.  The
  68. protocol has not formally been adopted
  69. to a 7-bit environment for the
  70. transmission of ASCII-only (or
  71. unpacked-hex) data, although it could
  72. be simply by having both ends agree to
  73. AND the protocol-dependent data with 7F
  74. hex before validating it.  I
  75. specifically am referring to the
  76. checksum, and the block numbers and
  77. their ones-complement.
  78.  
  79. Those wishing to maintain compatibility
  80. of the CP/M file structure, i.e., to
  81. allow modemming ASCII files to or from
  82. CP/M systems should follow this data
  83. format:
  84.  
  85. * ASCII tabs used (09H); tabs set
  86. every 8.
  87. * Lines terminated by CR/LF (0DH 0AH)
  88. * End-of-file indicated by ^Z, 1AH. 
  89. (one or more)
  90. * Data is variable length, i.e. should
  91. be considered a continuous stream
  92. of data bytes, broken into 128-byte
  93. chunks purely for the purpose of
  94. transmission.
  95. * A CP/M "peculiarity": If the data
  96. ends exactly on a 128-byte boundary,
  97. i.e.,
  98. CR in 127, and LF in 128, a
  99. subsequent sector containing the ^Z EOF
  100. character(s) is optional, but is
  101. preferred.  Some utilities or user
  102. prgrams
  103. still do not handle EOF without ^Zs.
  104. * The last block sent is no different
  105. from others, i.e., there is no "short
  106. block".
  107.  
  108.  3. MESSAGE BLOCK LEVEL PROTOCOL  Each
  109. block of the transfer looks like:
  110.  
  111. <SOH> <blk #> <255-blk #> <--128 data
  112. bytes--> <cksum> in which:  <SOH> = 01
  113. hex <blk #> = binary number, starts at
  114. 01 increments by 1, and wraps 0FFH to
  115. 00H (not to 01) <255-blk #> = blk #
  116. after going thru 8080 "CMA" instr,
  117. i.e., each bit complemented in the
  118. 8-bit block number.  Formally, this is
  119. the "ones complement".  <cksum> = the
  120. sum of the data bytes only.  Toss any
  121. carry.
  122.  
  123.  4. FILE LEVEL PROTOCOL
  124.  
  125. ---- 4A. COMMON TO BOTH SENDER AND
  126. RECEIVER:       All errors are retried
  127. 10 times.  For versions running with an
  128. operator (i.e., NOT with XMODEM), a
  129. message is typed after 10 errors asking
  130. the operator whether to "retry or
  131. quit".  Some versions of the protocol
  132. use <CAN>, ASCII ^X, to cancel
  133. transmission.  This was never adopted
  134. as a standard, as having a single
  135. "abort" character makes the
  136. transmission susceptible to false
  137. termination due to an <ACK> <NAK> or
  138. <SOH> being corrupted into a <CAN> and
  139. cancelling transmission.  The protocol may be considered "receiver driven",
  140. that is, the sender need not
  141. automatically re-transmit, although it
  142. does in the current implementations.
  143.  
  144. ---- 4B. RECEIVE PROGRAM
  145. CONSIDERATIONS:       The receiver has
  146. a 10-second timeout.  It sends a <NAK>
  147. every time it times out.  The
  148. receiver's first timeout, which sends a
  149. <NAK>, signals the transmitter to
  150. start.  Optionally, the receiver could
  151. send a <NAK> immediately, in case the
  152. sender was ready.  This would save the
  153. initial 10 second timeout.  However,
  154. the receiver MUST continue to timeout
  155. every 10 seconds in case the sender
  156. wasn't ready.  Once into a receiving a
  157. block, the receiver goes into a
  158. one-second timeout for each character
  159. and the checksum.  If the receiver
  160. wishes to <NAK> a block for any reason
  161. (invalid header, timeout receiving
  162. data), it must wait for the line to
  163. clear.  See "programming tips" for
  164. ideas.  Synchronizing:  If a valid
  165. block number is received, it will be:  
  166.     1) the expected one, in which case
  167. everything is fine; or   2) a repeat of
  168. the previously received block.  This
  169. should be considered OK, and only
  170. indicates that the receiver's <ACK> got
  171. glitched, and the sender
  172. re-transmitted;   3) any other block
  173. number indicates a fatal loss of
  174. synchronization, such as the rare case
  175. of the sender getting a line-glitch
  176. that looked like an <ACK>.  Abort the
  177. transmission, sending a <CAN>.
  178.  
  179. ---- 4C. SENDING PROGRAM
  180. CONSIDERATIONS.       While waiting for
  181. transmission to begin, the sender has
  182. only a single  very long timeout, say
  183. one minute.  In the current protocol,
  184. the sender has a 10 second timeout
  185. before retrying.  I suggest NOT doing
  186. this, and letting the protocol be
  187. completely receiver-driven.  This will
  188. be compatible with existing programs. 
  189. When the sender has no more data, it
  190. sends an <EOT>, and awaits an <ACK>,
  191. resending the <EOT> if it doesn't get
  192. one.  Again, the protocol could be
  193. receiver-driven, with the sender only
  194. having the high-level 1-minute timeout
  195. to abort.
  196.  
  197.  
  198.  5. DATA FLOW EXAMPLE INCLUDING ERROR
  199. RECOVERY
  200.  
  201. Here is a sample of the data flow,
  202. sendin' a 3-block message.  It includes
  203. the two most common line hits - a
  204. garbaged block, and an <ACK> reply
  205. getting garbaged.  <xx> represents the
  206. checksum byte
  207.  
  208. SENDERRECEIVERtimes out after 10 sec.
  209. <---<NAK> <SOH>01
  210. FE-data-<xx>---><---<ACK><SOH>02
  211. FD-data-<xx>--->(data gets line hit)
  212. <---<NAK> <SOH>02
  213. FD-data-<xx>---><---<ACK><SOH>03
  214. FC-data-<xx>--->(ack gets garbaged)
  215. <---<ACK> <SOH>03
  216. FC-data-<xx>---><---<ACK> <EOT>
  217. ---><---<ACK>
  218.  
  219.  6. PROGRAMMING TIPS.
  220.  
  221. * The character-receive subroutine should
  222. be called with a parameter specifying
  223. the number of seconds to wait.  The
  224. receiver should first call it with a
  225. time of 10, then <NAK> and try again,
  226. 10 times.  After receiving the <SOH>,
  227. the receiver should call the character
  228. receive subroutine with a 1-second
  229. timeout, for the remainder of the
  230. message and the <cksum>.  Since they
  231. are sent as a continuous stream, timing
  232. out of this implies a serious like
  233. glitch that caused, say, 127 characters
  234. to be seen instead of 128.
  235.  
  236. * When the receiver wishes to <NAK>,
  237. it should call a "PURGE" subroutine, to
  238. wait for the line to clear.  Recall the
  239. sender tosses any characters in its
  240. UART buffer immediately upon completing
  241. sending a block, to ensure no glitches
  242. were misinterpreted.  The most common
  243. technique is for "PURGE" to call the
  244. character receive subroutine,
  245. specifying a 1-second timeout, and
  246. looping back to PURGE until a timeout
  247. occurs.  The <NAK> is then sent,
  248. ensuring the other end will see it.
  249.  
  250. * You may wish to add code recommended
  251. by John Mahr to your character receive
  252. routine - to set an error flag if the
  253. UART shows framing error, or overrun. 
  254. This will help catch a few more
  255. glitches - the most common of which is
  256. a hit in the high bits of the byte in
  257. two consecutive bytes.  The <cksum>
  258. comes out OK since counting in 1-byte
  259. produces the same result of adding 80H
  260. + 80H as with adding 00H + 00H.
  261.  
  262.  7. OVERVIEW OF CRC OPTION
  263.  
  264. The CRC used in the Modem Protocol is
  265. an alternate form of block check which
  266. provides more robust error detection
  267. than the original checksum. Andrew S.
  268. Tanenbaum says in his book, Computer
  269. Networks, that the CRC-CCITT used by
  270. the Modem Protocol will detect all
  271. single and double bit errors, all
  272. errors with an odd number of bits, all
  273. burst errors of length 16 or less,
  274. 99.997% of 17-bit error bursts, and
  275. 99.998% of 18-bit and longer bursts.  
  276.    The changes to the Modem Protocol to
  277. replace the checksum with the CRC are
  278. straight forward.  If that were all
  279. that we did we would not be able to
  280. communicate between a program using the
  281. old checksum protocol and one using
  282. the new CRC protocol.  An initial
  283. handshake was added to solve this
  284. problem. The handshake allows a
  285. receiving program with CRC capability
  286. to determine whether the sending
  287. program supports the CRC option, and to
  288. switch it to CRC mode if it does. 
  289. This handshake is designed so that it
  290. will work properly with programs which
  291. implement only the original protocol. 
  292. A description of this handshake is
  293. presented in section 10. 
  294.  
  295.  8. MESSAGE BLOCK LEVEL PROTOCOL, CRC
  296. MODE
  297.  
  298. Each block of the transfer in CRC mode
  299. looks like:
  300.  
  301. <SOH><blk #><255-blk #><--128 data
  302. bytes--><CRC hi><CRC lo>       in
  303. which: <SOH> = 01 hex <blk #> =
  304. binary number, starts at 01 increments
  305. by 1, and wraps 0FFH to 00H (not to
  306. 01) <255-blk #> = ones complement of
  307. blk #. <CRC hi> = byte containing the
  308. 8 hi order coefficients of the CRC.
  309. <CRC lo> = byte containing the 8 lo
  310. order coefficients of the CRC.
  311.  
  312.  9. CRC CALCULATION
  313.  
  314.   ---- 9A. FORMAL DEFINITION OF THE CRC
  315. CALCULATION      To calculate the 16 bit
  316. CRC the message bits are considered to
  317. be the coefficients of a polynomial. 
  318. This message polynomial is first
  319. multiplied by X^16 and then divided by
  320. the generator polynomial (X^16 + X^12 +
  321. X^5 + 1) using modulo two arithemetic.
  322.  The remainder left after the division
  323. is the desired CRC.  Since a message
  324. block in the Modem Protocol is 128
  325. bytes or 1024 bits, the message
  326. polynomial will be of order X^1023. 
  327. The hi order bit of the first byte of
  328. the message block is the coefficient of
  329. X^1023 in the message polynomial. 
  330. The lo order bit of the last byte of
  331. the message block is the coefficient
  332. of X^0 in the message polynomial.
  333.  
  334.   ---- 9B. EXAMPLE OF CRC CALCULATION
  335. WRITTEN IN C /*      This function
  336. calculates the CRC used by the "Modem
  337. Protocol".  The first argument is a
  338. pointer to the message block.  The
  339. second argument is the number of bytes
  340. in the message block.  The message
  341. block used by the Modem Protocol
  342. contains 128 bytes.    The function
  343. return value is an integer which
  344. contains the CRC.  The lo order 16
  345. bits of this integer are the
  346. coefficients of the CRC.  The lo order
  347. bit is the lo order coefficient of the
  348. CRC. */ int calcrc(ptr, count) char
  349. *ptr; int count;      int crc, i;   
  350.  crc = 0;     while(--count >= 0)   
  351.       crc = crc ^ (int)*ptr++ << 8;  
  352.       for(i = 0; i < 8; ++i)          
  353.   if(crc & 0x8000)                 crc
  354. = crc << 1 ^ 0x1021;             else
  355.                 crc = crc << 1;       
  356.       return (crc & 0xFFFF);     
  357.  
  358. 10. FILE LEVEL PROTOCOL, CHANGES FOR
  359. COMPATIBILITY
  360.  
  361.   ---- 10A. COMMON TO BOTH SENDER AND
  362. RECEIVER:      The only change to the
  363. File Level Protocol for the CRC option
  364. is the initial handshake which is used
  365. to determine if both the sending and
  366. the receiving programs support the CRC
  367. mode.  All Modem Programs should
  368. support the checksum mode for
  369. compatibility with older versions.    
  370.  A receiving program that wishes to
  371. receive in CRC mode implements the
  372. mode setting handshake by sending a <C>
  373. in place of the initial <NAK>.  If the
  374. sending program supports CRC mode it
  375. will recognize the <C> and will set
  376. itself into CRC mode, and respond by
  377. sending the first block as if a <NAK>
  378. had been received.  If the sending
  379. program does not support CRC mode it
  380. will not respond to the <C> at all. 
  381. After the receiver has sent the <C> it
  382. will wait up to 3 seconds for the
  383. <SOH> that starts the first block.  If
  384. it receives a <SOH> within 3 seconds
  385. it will assume the sender supports CRC
  386. mode and will proceed with the file
  387. exchange in CRC mode.  If no <SOH> is
  388. received within 3 seconds the receiver
  389. will switch to checksum mode, send a
  390. <NAK>, and proceed in checksum mode. 
  391. If the receiver wishes to use checksum
  392. mode it should send an initial <NAK>
  393. and the sending program should respond
  394. to the <NAK> as defined in the
  395. original Modem Protocol.  After the
  396. mode has been set by the initial <C> or
  397. <NAK> the protocol follows the
  398. original Modem Protocol and is
  399. identical whether the checksum or CRC
  400. is being used.
  401.  
  402.   ---- 10B. RECEIVE PROGRAM CONSIDERATIONS:
  403.      There are at least 4 things that
  404. can go wrong with the mode setting
  405. handshake:
  406.  
  407.       1. the initial <C> can be garbled
  408. or lost.
  409.       2. the initial <SOH> can be
  410. garbled.
  411.       3. the initial <C> can be changed
  412. to a <NAK>.
  413.       4. the initial <NAK> from a
  414. receiver which wants to receive in
  415. checksum can be changed
  416. to a <C>.
  417.  
  418.       The first problem can be solved
  419. if the receiver sends a second <C>
  420. after it times out the first time. 
  421. This process can be repeated several
  422. times.  It must not be repeated too
  423. many times before sending a <NAK> and
  424. switching to checksum mode or a
  425. sending program without CRC support may
  426. time out and abort.  Repeating the
  427. <C> will also fix the second problem if
  428. the sending program cooperates by
  429. responding as if a <NAK> were received
  430. instead of ignoring the extra <C>.
  431.  
  432.       It is possible to fix problems 3
  433. and 4 but probably not worth the
  434. trouble since they will occur very
  435. infrequently.  They could be fixed by
  436. switching modes in either the sending
  437. or the receiving program after a
  438. large number of successive <NAK>s. 
  439. This solution would risk other
  440. problems however.
  441.  
  442.   ---- 10C. SENDING PROGRAM
  443. CONSIDERATIONS.      The sending
  444. program should start in the checksum
  445. mode.  This will insure compatibility with checksum only receiving programs. 
  446. Anytime a <C> is received before the
  447. first <NAK> or <ACK> the sending
  448. program should set itself into CRC
  449. mode and respond as if a <NAK> were
  450. received.  The sender should respond
  451. to additional <C>s as if they were <NAK>s
  452. until the first <ACK> is received. 
  453. This will assist the receiving program
  454. in determining the correct mode when
  455. the <SOH> is lost or garbled.  After
  456. the first <ACK> is received the
  457. sending program should ignore <C>s. 
  458.  
  459. 11. DATA FLOW EXAMPLES WITH CRC
  460. OPTION
  461.  
  462.   ---- 11A. RECEIVER HAS CRC OPTION,
  463. SENDER DOESN'T      Here is a data
  464. flow example for the case where the
  465. receiver requests transmission in the
  466. CRC mode but the sender does not
  467. support the CRC option. This example
  468. also includes various transmission
  469. errors.  <xx> represents the checksum
  470. byte.
  471.  
  472. SENDERRECEIVER        <---           
  473. <C>times out after 3 sec.
  474. <---<NAK><SOH>01
  475. FE-data-<xx>---><---<ACK><SOH>02
  476. FD-data-<xx>--->(data gets line hit)
  477. <---<NAK><SOH>02 FD-data-<xx>---><---<ACK><SOH>03
  478. FC-data-<xx>--->(ack gets garbaged)
  479. <---<ACK> times out after 10
  480. seconds<---<NAK><SOH>03
  481. FC-data-<xx>--->
  482. <---<ACK><EOT>---><---<ACK>
  483.  
  484.  ---- 11B. RECEIVER AND SENDER BOTH
  485. HAVE CRC OPTION      Here is a data
  486. flow example for the case where the
  487. receiver requests transmission in the
  488. CRC mode and the sender supports the
  489. CRC option.  This example also
  490. includes various transmission errors. 
  491. <xxxx> represents the 2 CRC bytes.
  492.  
  493. SENDERRECEIVER
  494. <---<C><SOH>01 FE-data-<xxxx>---><---<ACK><SOH>02
  495. FD-data-<xxxx>--->(data gets line
  496. hit)
  497. <---<NAK><SOH>02
  498. FD-data-<xxxx>---><---<ACK><SOH>03
  499. FC-data-<xxxx>--->(ack gets garbaged)
  500. <---<ACK> times out after 10
  501. seconds<---<NAK><SOH>03
  502. FC-data-<xxxx>--->
  503. <---<ACK><EOT>---><---<ACK>
  504.  
  505. Apendix 1. MODEM PROTOCOL STATE TABLE
  506.  
  507. ---- A1A. CONSIDERATIONS      The
  508. Modem Protocol can be considered a
  509. group of states and transitions.
  510. States represent certain actions taken
  511. by the program and certain expected
  512. results for those actions.  The
  513. transitions are actions taken in
  514. reponse to a particular result,
  515. actions which can result in another
  516. state.
  517.  
  518.       The state table shows the
  519. complete set of states for a program
  520. with the CRC option.  Programs without
  521. this option should ignore the <C>
  522. result in the Send-Init state and also
  523. ignore the Rec-Init-CRC state.  There
  524. is a minor difference between the Data
  525. Flow Examples given by Ward
  526. Christensen and John Byrns.  This
  527. difference is the reaction of the
  528. sender when the <ACK> to a block is garbled
  529. (not lost).  In Ward's example the
  530. sender reacts by retransmitting the
  531. current block.  In John's example the
  532. garbled <ACK> is ignored and nothing
  533. happens until the reciever has a
  534. timeout and sends a <NAK>.  The state
  535. table uses the first method of
  536. reacting to a garbled <NAK>.  This is
  537. the recommended method as the
  538. retransmission of a data block, even
  539. at the lowest baud rates, takes
  540. considerably less time than waiting
  541. for a timeout from the receiver.  In
  542. the State Table, n is the current block
  543. number (therefore n-1 is, of course,
  544. the previous block number); r is the
  545. retry counter and c is the CRC
  546. handshake retry counter.  The actions
  547. n+, r+ and c+ are incrementing the
  548. appropriate counter.  It should be
  549. noted that the action n+ will always
  550. cause r = 0 or, to put it another
  551. way, whenever a block is successfully
  552. sent and recieved the retry counter is
  553. reset.  When a r+ action causes r to
  554. reach the threshold, an error is
  555. generated and the program is aborted. 
  556. A Result in angle brackets (i.e. < >)
  557. is the reciept of that character. A
  558. Result of "Block..." is the reciept of
  559. a complete, valid data block. Results
  560. of Other and Timeout are the reciept of
  561. any unlisted input (invalid or
  562. incomplete blocks included) and the
  563. occurance of a timeout in the character
  564. recieve routine, respectively.      A
  565. specific check is made for <EOT> when
  566. expecting the first data block. This
  567. is because some installations (e.g.
  568. CompuServe) will send an <EOT> to
  569. signal that the processor is too busy
  570. to successfully transfer a file.
  571.  
  572. ---- A1B. STATE TABLE
  573.  
  574. StateAction on entryResultAction
  575. on resultNext State
  576. Send-InitSet cksum
  577. mode,n=0<NAK>Get data for 1st
  578. block,n+
  579. Send-Data<C> Set CRC mode,get dat
  580. for 1st blk,n+Send-Data
  581. Other r+Send-InitTimeout ErrorAbortSend-Data
  582. Send
  583. Block n<ACK> Get dat for nxt blk, n+
  584. Send-Data, or Send-EOT,if EOF<NAK>
  585. or
  586. Other r+Send-DataTimeout
  587. ErrorAbortSend-EOT
  588. Send <EOT><ACK> --Exit
  589. Other r+Send-EOTTimeout ErrorAbort
  590. Rec-Init-CRCSet CRC mode,Send <C>,
  591. n=1Blk nStore data,send <ACK>, n+
  592. Rec-Data<EOT>ErrorAbort
  593. Other r+Rec-Init-CRCTimeout c+
  594. Rec-Init-CRCc+ thresholdSet cksum
  595. mode, r=0Rec-Init-Cksm
  596. Rec-Init-CksmSend <NAK>All--Rec-Data
  597. Rec-Data--Block nStore data, send
  598. <ACK>,n+
  599. Rec-DataBlock n-1Send <ACK>, r+
  600. Rec-Data<EOT>If n = 1, ErrorAbort
  601.  
  602.  
  603.  This version of the document was
  604.